package com.xuhuan.juc.callable;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @Author huan.xu
 * @Date 2021/12/29 15:05
 * @Description callable获取线程的方式
 */
@Slf4j
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask futureTask = new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                log.info("线程【{}】进入", Thread.currentThread().getName());
                TimeUnit.SECONDS.sleep(4);
                return 1024;
            }
        });
        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start();
        log.info("main方法执行完毕");
        log.info("线程返回结果：{}", futureTask.get());
    }
}
